<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
    computed: {
        disabledOrNot() {
            return function (_label) {
                let weekShowFlag = false;
                for (let arr of this.finalWeek) {
                    if (arr === _label) {
                        weekShowFlag = true;
                    }
                }
                if (weekShowFlag) {
                    return true;
                }
            };
        },
        counter() {
            let _counter = 0;
            for (let arr of this.timeArr) {
                if (arr.deleteFlag) {
                    _counter++;
                }
            }
            if (_counter >= 3) {
                return false;
            } else {
                return true;
            }
        },
        showArr() {
            return this.timeArr.filter((item) => item.deleteFlag === true);
        },
    },
    data() {
        return {
            startTime: "",
            endTime: "",

            finalWeek: [],
            // 窗口显示
            boxVisible: false,

            checkList: [], // 收集周期数据
            timeValue: "", // 收集时间段数据

            timeArr: [], //

            weekList: [],

            // 判断时间段是否已被选择过
            selectTime: {
                value: [],
                list: {},
            },
            _isFlag: false,

            // 回显的数据
            dataItem: {
                timeArr: [],
                weekList: [],
            },
        };
    },
    methods: {
        // 星期转化为数字
        changeToNum(_arr) {
            let res = [];
            for (let arr of _arr) {
                switch (arr) {
                    case "星期一":
                        res.push(0);
                        break;
                    case "星期二":
                        res.push(1);
                        break;
                    case "星期三":
                        res.push(2);
                        break;
                    case "星期四":
                        res.push(3);
                        break;
                    case "星期五":
                        res.push(4);
                        break;
                    case "星期六":
                        res.push(5);
                        break;
                    case "星期日":
                        res.push(6);
                        break;
                }
            }
            return res;
        },
        // 数字转星期
        changeToWeek(_arr) {
            let res = [];
            for (let arr of _arr) {
                switch (arr) {
                    case 0:
                        res.push("星期一");
                        break;
                    case 1:
                        res.push("星期二");
                        break;
                    case 2:
                        res.push("星期三");
                        break;
                    case 3:
                        res.push("星期四");
                        break;
                    case 4:
                        res.push("星期五");
                        break;
                    case 5:
                        res.push("星期六");
                        break;
                    case 6:
                        res.push("星期日");
                        break;
                }
            }
            return res;
        },

        submit() {
            if (this.checkList.length > 0 && this.timeArr.length > 0) {
                let _weekArr = [];
                for (let arr of this.checkList) {
                    _weekArr.push(arr);
                }
                let res = this.changeToNum(_weekArr);
                this.dataItem.weekList = this.weekInOrder(res); // 最终周次数据
                for (let arr of this.showArr) {
                    this.dataItem.timeArr.push({
                        begin: arr.begin,
                        end: arr.end,
                    }); // 最终时间段数据
                }
                console.error();
                this.checkList = [];
                let _d = {
                    index: this.index,
                    item: JSON.parse(JSON.stringify(this.dataItem)),
                    list: JSON.parse(JSON.stringify(this.list)),
                };

                if (this.index > -1) {
                    _d.list[this.index] = _d.item;
                }
                else{
                    _d.list.push(_d.item)
                }
                this.onSuccess(_d);
                this.editBoxClose();
            } else if (this.checkList.length === 0) {
                this.$vFN.Tools.messageError("请选择周次");
            } else {
                this.$vFN.Tools.messageError("请选择时间段");
            }
        },

        addTime() {
            if (this.timeValue.length === 0) {
                this.$vFN.Tools.messageError("请选择时间段");
            } else {
                if (this.counter) {
                    let _start = this.timeValue[0].split(":");
                    let _end = this.timeValue[1].split(":");
                    let _a = parseInt(_start[0]);
                    let _b = parseInt(_start[1]);
                    let _c = parseInt(_end[0]);
                    let _d = parseInt(_end[1]);
                    this.addFunc(_a, _b, _c, _d);
                    if (!this._isFlag) {
                        this.timeArr.push({
                            begin: this.timeValue[0],
                            end: this.timeValue[1],
                            deleteFlag: true,
                        });
                        this._isFlag = false;
                        this.timeValue = "";
                        this.timeInOrder();
                    } else {
                        this.$vFN.Tools.messageError("已存在");
                        this.timeValue = "";
                    }
                } else {
                    this.$vFN.Tools.messageError("最多可以选择三个");
                }
            }
        },

        // 星期排序
        weekInOrder(arr) {
            var interval = parseInt(arr.length / 2); //分组间隔设置
            while (interval > 0) {
                for (var i = 0; i < arr.length; i++) {
                    var n = i;
                    while (arr[n] < arr[n - interval] && n > 0) {
                        var temp = arr[n];
                        arr[n] = arr[n - interval];
                        arr[n - interval] = temp;
                        n = n - interval;
                    }
                }
                interval = parseInt(interval / 2);
            }
            return arr;
        },

        // 时间段排序
        timeInOrder() {
            let timeArr = this.timeArr;
            let res = [];
            let num = "";
            for (let i = 0; i < timeArr.length; i++) {
                num = timeArr[i].begin.split("");
                res[i] = parseInt(num[0] * 60 + num[1]);
            }
            // console.log(res)
            let temp;
            let _temp;
            for (let i = 0; i < res.length - 1; i++) {
                for (let j = 0; j < res.length - 1; j++) {
                    if (res[j] > res[j + 1]) {
                        _temp = res[j];
                        res[j] = res[j + 1];
                        res[j + 1] = _temp;

                        temp = timeArr[j];
                        timeArr[j] = timeArr[j + 1];
                        timeArr[j + 1] = temp;
                    }
                }
            }
            this.timeArr = timeArr;
            // console.log(timeArr)
        },
        addFunc(a1, a2, b1, b2) {
            this._isFlag = this.isTime(a1, a2, b1, b2);
            if (!this._isFlag) {
                for (let i = a1 * 60 + a2; i < b1 * 60 + b2; i++) {
                    this.selectTime.list[i] = true;
                }
            }
        },
        isTime(a1, a2, b1, b2) {
            let _is = false;
            for (let i = a1 * 60 + a2; i <= b1 * 60 + b2; i++) {
                if (this.selectTime.list[i]) {
                    _is = true;
                }
            }
            return _is;
        },

        deleteItem(_item) {
            _item.deleteFlag = false;
            this.resetTime(_item.begin, _item.end, false);
        },
        // 重置时间数
        resetTime(begin, end, flag) {
            let _start = begin.split(":");
            let _end = end.split(":");
            let _a = parseInt(_start[0]);
            let _b = parseInt(_start[1]);
            let _c = parseInt(_end[0]);
            let _d = parseInt(_end[1]);
            if (!flag) {
                console.log("删除");
                for (let i = _a * 60 + _b; i < _c * 60 + _d; i++) {
                    this.selectTime.list[i] = false;
                }
            }
            if (flag) {
                console.log("初始化");
                for (let i = _a * 60 + _b; i < _c * 60 + _d; i++) {
                    this.selectTime.list[i] = true;
                }
                this.timeArr.push({ begin: begin, end: end, deleteFlag: true });
            }
        },

        // 成功回调
        onSuccess() {},
        // 取消回调
        onCancel() {},
        // 失败回调
        onError() {},
        // 关闭窗口
        editBoxClose() {
            this.boxVisible = false;
        },
        // 取消
        editBoxCancel() {
            this.editBoxClose();
        },
    },
    watch: {},
    mounted() {
        if (this.index > -1) {
            console.log("编辑的时候");
            // 遍历所有数据，筛选出所有已选的周次
            let _weekList = [];
            for (let arr of this.list) {
                for (let week of arr.weekList) {
                    _weekList.push(week);
                }
            }

            // 遍历当前编辑的数据，筛选出当前数据已选的周次和时间段
            let _editList = [];
            let _selected = this.list[this.index];

            for (let week of _selected.weekList) {
                _editList.push(week); // 当前编辑的数据选择的周次
            }

            for (let time of _selected.timeArr) {
                this.resetTime(time.begin, time.end, true); // 设置不可添加的时间
            }

            this.checkList = this.changeToWeek(_editList); // 之前选择过的周次显示出来

            // 判断当前编辑的周次，把当前的可选择周次变为不可选择
            for (let i = 0; i <= _weekList.length; i++) {
                for (let j = 0; j <= _editList.length; j++) {
                    if (_weekList[i] === _editList[j]) {
                        delete _weekList[i]; // 编辑的时候可以选择
                    }
                }
            }
            this.finalWeek = _weekList;
        } else {
            console.log("添加的时候");
            // 遍历所有数据，筛选出所有已选的周次
            let _weekList = [];
            for (let arr of this.list) {
                for (let week of arr.weekList) {
                    _weekList.push(week);
                }
            }
            this.finalWeek = _weekList;
        }
        this.boxVisible = true;
    },
    beforeCreate() {},
};
</script>
